﻿using FFTWSharp;
using System;
using System.Collections.Generic;
using System.IO;

namespace TestDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                int sampleSize = 131072;

                FFTWWrapper fftwWrapper = new FFTWWrapper();

                float[] fin = new float[sampleSize];//输入时域波形
                double[] amp = new double[sampleSize / 2];//返回的功率谱
                double[] pha = new double[sampleSize / 2];//返回的相位谱
                double[] coorX = new double[sampleSize / 2];//返回的相位谱

                //从txt文件中读取数据
                //string path = @"C:\Users\jm_yu\Desktop\vibxpert2\DataAnalysis\";
                //fin = readData(path + "timewave-1000-90-131072-25600.txt");

                //生成模拟数据
                float f = 100;//Hz
                float fs = 4096;//Hz
                float dt = 1 / fs;
                for (int i = 0; i < sampleSize; i++)
                {
                    fin[i] = (float)Math.Sin(2 * Math.PI * f * dt * i);
                }

                //调用算法
                int start = System.Environment.TickCount;
                //fs=25600
                fftwWrapper.FFTAmpCompute(fin, fs, ref amp, ref pha, ref coorX);
                long ticks = (System.Environment.TickCount - start);
                Console.WriteLine($"time={ticks}");

                //保存数据
                saveData("amp.txt", amp);
                saveData("pha.txt", pha);
                saveData("coorX.txt", coorX);

                Console.ReadLine();
            }

            {
                //int sampleSize = 131072;
                //bool forgetWisdom = true;
                //if (forgetWisdom)
                //{
                //    fftwf.fftwf_forget_wisdom();
                //}
                //else
                //{
                //    Console.WriteLine("Importing wisdom (wisdom speeds up the plan creation process, if that plan was previously created at least once)");
                //    fftwf.import_wisdom_from_filename("wisdom.wsd");
                //}

                //// create two managed arrays, possibly misalinged
                //// n*2 because we are dealing with complex numbers
                //float[] fin = new float[sampleSize * 2];
                //float[] fout = new float[sampleSize * 2];


                //// create a managed plan as well
                //fftwf_complexarray mfin, mfout;
                //mfin = new fftwf_complexarray(fin);
                //mfout = new fftwf_complexarray(fout);

                //// create a managed plan as well
                //fftwf_plan mplan1, mplan2, mplan3;
                //mplan1 = fftwf_plan.dft_1d(sampleSize, mfin, mfout, fftw_direction.Forward, fftw_flags.Estimate);
                //mplan2 = fftwf_plan.dft_1d(sampleSize, mfin, mfout, fftw_direction.Forward, fftw_flags.Measure);
                //mplan3 = fftwf_plan.dft_1d(sampleSize, mfin, mfout, fftw_direction.Forward, fftw_flags.Patient);


                //// fill our arrays with an arbitrary complex sawtooth-like signal
                //for (int i = 0; i < sampleSize * 2; i++) fin[i] = i % 50;
                //for (int i = 0; i < sampleSize * 2; i++) fout[i] = i % 50;
                //mfin.SetData(fin);
                ////执行计划
                //mplan1.Execute();
                //float[] realin = mfin.GetData_Real();
                //float[] real = mfout.GetData_Real();
                //float[] all = mfout.GetData_Float();
                //Complex[] complex = mfout.GetData_Complex();

                //// a: adds, b: muls, c: fmas
                //double a = 0, b = 0, c = 0;
                //fftwf.flops(mplan3.Handle, ref a, ref b, ref c);
                //Console.WriteLine($"a={a},b={b},c={c}");

                //System.Console.WriteLine("\nDone. Press any key to exit.");
                //String str = System.Console.ReadLine();
            }
        }

        public static float[] readData(string path)
        {
            int counter = 0;
            string line;
            List<float> data = new List<float>();

            System.IO.StreamReader file =
                new System.IO.StreamReader(path);
            while ((line = file.ReadLine()) != null)
            {
                data.Add(Convert.ToSingle(line));
                counter++;
            }

            file.Close();
            return data.ToArray();
        }

        public static void saveData(string path, double[] data)
        {
            FileInfo myFile = new FileInfo(path);
            StreamWriter sW = myFile.CreateText();

            for (int i = 0; i < data.Length; i++)
            {
                sW.WriteLine(data[i].ToString());
            }

            sW.Close();
        }

    }
}
